const express = require("express");
const cors = require("cors");
const mysql = require("mysql");

const app = express();

app.use(cors());
app.use(express.urlencoded({ extended: false }));

const conn = mysql.createConnection({
	host: "localhost", 
	user: "root", 
    prot: "3306",
	password: "abc123456",
});

conn.connect();

app.get("/ddinfo", (req, res) => {
	conn.query("SELECT * FROM workshop.ddq WHERE ddID = " + req.query[0], (error, result) => {
		if (error) throw error;
		res.send({
			message: "success",
			status: 200,
			data: result,
		});
	});
});

app.get("/orderinfo", (req, res) => {
	conn.query("SELECT * FROM workshop.ddq", (error, result) => {
		if (error) throw error;
		res.send({
			message: "success",
			status: 200,
			data: result,
		});
	});
});

app.get("/userinfo", (req, res) => {
	conn.query("SELECT * FROM workshop.user", (error, result) => {
		if (error) throw error;
		res.send({
			message: "success",
			status: 200,
			data: result,
		});
	});
});

app.get("/insertdd",(req,res) => {
	// console.log(req.query)
	// console.log(Object.values(req.query).join("','"))
	conn.query("INSERT INTO `workshop`.`ddq` (`" + Object.keys(req.query).join("`,`") + "`) VALUES ('"+ Object.values(req.query).join("','") +"');")
});

app.listen(9000, () => {
	console.log("success in 9000");
});
